// pages/sharebox/shareUser/shareUser.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    msgid: 0,
    aimg: '',
    dname: '',
    name: '',
    deviceid: 0,
    tab: 1,
    mid: 0,
    uid: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    that.setData({
      tab: options.tab,
    });

    if (options.tab == 1) {
      that.setData({
        msgid: options.id,
        aimg: options.aimg,
        dname: options.dname,
        name: options.name
      })
    } else if (options.tab == 2) {
      that.setData({
        deviceid: options.deviceid,
        uid: options.uid,
        tab: 2
      })
      that.getopenid();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    var tab = that.data.tab
    if (tab == 2) {
      that.getopenid();
    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },


  // 接受
  accept() {
    var that = this;
    app.ajax({
      url: "User/Shared/disposeShared",
      data: {
        id: that.data.msgid,
        state: 3
      },
      success: function(res) {
        if (res.data.code == 1000) {
          app.showToast('已接受共享,1秒后自动跳转');
          setTimeout(function() {
            wx.navigateTo({
              url: '../deviceList/deviceList',
            })
          }, 1000)
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },

  refuse() {
    var that = this;
    app.ajax({
      url: "User/Shared/disposeShared",
      data: {
        id: that.data.msgid,
        state: 2
      },
      success: function(res) {
        if (res.data.code == 1000) {
          app.showToast('已拒绝共享,1秒后自动跳转');
          setTimeout(function() {
            wx.navigateTo({
              url: '../deviceList/deviceList',
            })
          }, 1000)
        } else {
          app.showToast(res.data.message);
        }
      }
    })
  },

  getopenid() {
    var that = this;
    var openid = wx.getStorageSync('openid');
    that.getregister(openid)
  },

  getregister(openid) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    app.ajax({
      url: "User/Shared/register",
      data: {
        openid: openid,
      },
      success: function(res) {
        if (res.data.code == 1000) {
          that.setData({
            mid: res.data.data
          })
          that.getinitator()
        }
      }
    })
  },

  getinitator() {
    var that = this;
    app.ajax({
      url: "User/Shared/initiator",
      data: {
        user_id: that.data.uid,
        device_id: that.data.deviceid
      },
      success: function(res) {
        if (res.data.code == 1000) {
          that.setData({
            aimg: res.data.data.avatar_img,
            name: res.data.data.name,
            dname: res.data.data.device_name
          })
          that.getmsg()
        }
      }
    })
  },

  getmsg() {
    var that = this;
    app.ajax({
      url: "User/Shared/addShareMessage",
      data: {
        user_id: that.data.uid,
        device_id: that.data.deviceid,
        recipient: that.data.mid
      },
      success: function(res) {
        wx.hideLoading()
        if (res.data.code == 1000) {
          that.setData({
            msgid: res.data.data
          })
        } else {
          app.showToast(res.data.message +'1秒后自动跳转');
          setTimeout(function () {
            wx.switchTab({
              url: '../../home/home'
            });
          }, 1000)
          
        }
      }
    })
  }

})